// This is a basic Flutter widget test.
//
// To perform an interaction with a widget in your test, use the WidgetTester
// utility that Flutter provides. For example, you can send tap and scroll
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'dart:io';

import 'package:crypto/crypto.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mybaseplus/base/tool/entitylog/MyEntityTestRun.dart';
import 'package:mybaseplus/base/tool/entitylog/MyLanguageTestRun.dart';
import 'package:mybaseplus/base/tool/file/FFile.dart';
import 'dart:convert';

///将翻译好的文件写入到相应语言文件中
Future<void> main() async {

  const String fileName = "zh-Hant.json";

  const String md5MapFile = "/Users/os/spotify/spotify/test/create/language/translate/md5.json";
  const String targetFile = "/Users/os/spotify/spotify/assets/language/$fileName";

  String md5MapFileContent = await FFile.readAsString(md5MapFile);
  Map<String,dynamic> md5Map = json.decode(md5MapFileContent);

  String targetFileContent = await FFile.readAsString(targetFile);
  Map<String,dynamic> targetFileMap = json.decode(targetFileContent);

  convertFile("", targetFileMap,md5Map);

  String jsonMap = json.encode(targetFileMap);
  bool success = await FFile.writeContentByPath(filePath: targetFile, content: jsonMap,mode: FileMode.write);
  print("写入状态: $success");
}

void convertFile(String outKey, Map<String,dynamic> targetFileMap,Map<String,dynamic> md5Map){

  targetFileMap.forEach((key, value) {

    String inKey = md5.convert(utf8.encode("$outKey$key")).toString();
    if (value is String) {
      if (md5Map.containsKey(inKey)) {
        targetFileMap[key] = md5Map[inKey];
      }
    }  else if (value is Map) {
      Map<String,dynamic> valueMap = value as Map<String,dynamic>;
      convertFile(inKey, valueMap, md5Map);
    }
  });
}

